import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        Scanner scanner = new Scanner(System.in);
        ClassManager classManager = new ClassManager();
        GroupManager groupManager = new GroupManager();
        StudentManager studentManager = new StudentManager();
        RandomPicker randomPicker = new RandomPicker(classManager, groupManager, studentManager);

        studentManager.setRandomPicker(randomPicker);
        groupManager.setRandomPicker(randomPicker);

        JFrame mainFrame = new JFrame("班级管理系统");
        mainFrame.setSize(400, 300);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setLayout(new GridLayout(5, 1, 10, 10));

        // 创建主菜单按钮
        JButton classButton = new JButton("班级管理");
        JButton groupButton = new JButton("小组管理");
        JButton studentButton = new JButton("学生管理");
        JButton randomButton = new JButton("随机点名系统");
        JButton exitButton = new JButton("退出");

        // 班级管理按钮事件
        classButton.addActionListener(e -> {
            String[] options = {"新增班级", "修改班级名称", "班级列表"};
            int choice = JOptionPane.showOptionDialog(mainFrame,
                    "请选择操作", "班级管理",
                    JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
                    null, options, options[0]);

            switch (choice) {
                case 0: // 新增班级
                    String newClassName = JOptionPane.showInputDialog(mainFrame, "请输入班级名称:");
                    if (newClassName != null && !newClassName.trim().isEmpty()) {
                        classManager.getClasses().add(newClassName);
                        JOptionPane.showMessageDialog(mainFrame, "班级 " + newClassName + " 已新增");
                    }
                    break;
                case 1: // 修改班级名称
                    String[] classes = classManager.getClasses().toArray(new String[0]);
                    String selectedClass = (String) JOptionPane.showInputDialog(mainFrame,
                            "选择要修改的班级:", "修改班级名称",
                            JOptionPane.QUESTION_MESSAGE, null, classes, classes[0]);

                    if (selectedClass != null) {
                        String newName = JOptionPane.showInputDialog(mainFrame, "请输入新的班级名称:", selectedClass);
                        if (newName != null && !newName.trim().isEmpty()) {
                            int index = classManager.getClasses().indexOf(selectedClass);
                            classManager.getClasses().set(index, newName);
                            JOptionPane.showMessageDialog(mainFrame, "班级名称已修改为: " + newName);
                        }
                    }
                    break;
                case 2: // 班级列表
                    StringBuilder classList = new StringBuilder("班级列表:\n");
                    for (String className : classManager.getClasses()) {
                        classList.append(className).append("\n");
                    }
                    JOptionPane.showMessageDialog(mainFrame, classList.toString());
                    break;
            }
        });

        // 小组管理按钮事件
        groupButton.addActionListener(e -> {
            String[] options = {"新增小组", "修改小组", "小组列表"};
            int choice = JOptionPane.showOptionDialog(mainFrame,
                    "请选择操作", "小组管理",
                    JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
                    null, options, options[0]);

            switch (choice) {
                case 0: // 新增小组
                    String newGroupName = JOptionPane.showInputDialog(mainFrame, "请输入小组名称:");
                    if (newGroupName != null && !newGroupName.trim().isEmpty()) {
                        groupManager.getGroups().add(newGroupName);
                        JOptionPane.showMessageDialog(mainFrame, "小组 " + newGroupName + " 已新增");
                    }
                    break;
                case 1: // 修改小组
                    String[] groups = groupManager.getGroups().toArray(new String[0]);
                    if (groups.length == 0) {
                        JOptionPane.showMessageDialog(mainFrame, "暂无小组可修改");
                        return;
                    }
                    String selectedGroup = (String) JOptionPane.showInputDialog(mainFrame,
                            "选择要修改的小组:", "修改小组",
                            JOptionPane.QUESTION_MESSAGE, null, groups, groups[0]);

                    if (selectedGroup != null) {
                        String[] modifyOptions = {"修改名称", "记录成绩"};
                        int modifyChoice = JOptionPane.showOptionDialog(mainFrame,
                                "请选择修改操作", "修改小组",
                                JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
                                null, modifyOptions, modifyOptions[0]);

                        if (modifyChoice == 0) {
                            String newName = JOptionPane.showInputDialog(mainFrame, "请输入新的小组名称:", selectedGroup);
                            if (newName != null && !newName.trim().isEmpty()) {
                                int index = groupManager.getGroups().indexOf(selectedGroup);
                                groupManager.getGroups().set(index, newName);
                                JOptionPane.showMessageDialog(mainFrame, "小组名称已修改为: " + newName);
                            }
                        } else if (modifyChoice == 1) {
                            String scoreStr = JOptionPane.showInputDialog(mainFrame, "请输入小组成绩 (0-100):");
                            try {
                                int score = Integer.parseInt(scoreStr);
                                randomPicker.recordGroupScore(selectedGroup, score);
                                JOptionPane.showMessageDialog(mainFrame, "已记录小组 " + selectedGroup + " 的成绩为: " + score);
                            } catch (NumberFormatException ex) {
                                JOptionPane.showMessageDialog(mainFrame, "请输入有效的数字");
                            }
                        }
                    }
                    break;
                case 2: // 小组列表
                    StringBuilder groupList = new StringBuilder("小组列表:\n");
                    for (String groupName : groupManager.getGroups()) {
                        groupList.append(groupName).append("\n");
                    }
                    JOptionPane.showMessageDialog(mainFrame, groupList.toString());
                    break;
            }
        });

        // 学生管理按钮事件
        studentButton.addActionListener(e -> {
            String[] options = {"新增学生", "维护学生", "学生列表"};
            int choice = JOptionPane.showOptionDialog(mainFrame,
                    "请选择操作", "学生管理",
                    JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
                    null, options, options[0]);

            switch (choice) {
                case 0: // 新增学生
                    String newStudentName = JOptionPane.showInputDialog(mainFrame, "请输入学生姓名:");
                    if (newStudentName != null && !newStudentName.trim().isEmpty()) {
                        studentManager.getStudents().add(newStudentName);
                        JOptionPane.showMessageDialog(mainFrame, "学生 " + newStudentName + " 已新增");
                    }
                    break;
                case 1: // 维护学生
                    String[] students = studentManager.getStudents().toArray(new String[0]);
                    if (students.length == 0) {
                        JOptionPane.showMessageDialog(mainFrame, "暂无学生可维护");
                        return;
                    }
                    String selectedStudent = (String) JOptionPane.showInputDialog(mainFrame,
                            "选择要维护的学生:", "维护学生",
                            JOptionPane.QUESTION_MESSAGE, null, students, students[0]);

                    if (selectedStudent != null) {
                        String scoreStr = JOptionPane.showInputDialog(mainFrame, "请输入学生成绩:");
                        try {
                            double score = Double.parseDouble(scoreStr);
                            randomPicker.recordStudentScore(selectedStudent, score);
                            JOptionPane.showMessageDialog(mainFrame, "已记录学生 " + selectedStudent + " 的成绩为: " + score);
                        } catch (NumberFormatException ex) {
                            JOptionPane.showMessageDialog(mainFrame, "请输入有效的数字");
                        }
                    }
                    break;
                case 2: // 学生列表
                    StringBuilder studentList = new StringBuilder("学生列表:\n");
                    for (String studentName : studentManager.getStudents()) {
                        studentList.append(studentName).append("\n");
                    }
                    JOptionPane.showMessageDialog(mainFrame, studentList.toString());
                    break;
            }
        });

        // 随机点名按钮事件
        randomButton.addActionListener(e -> {
            String[] options = {
                    "随机抽取小组",
                    "随机在小组中抽取学生",
                    "随机在班级里抽取学生",
                    "给小组评分",
                    "记录学生考勤",
                    "导出成绩"
            };
            int choice = JOptionPane.showOptionDialog(mainFrame,
                    "请选择操作", "随机点名系统",
                    JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
                    null, options, options[0]);

            switch (choice) {
                case 0: // 随机抽取小组
                    if (groupManager.getGroups().isEmpty()) {
                        JOptionPane.showMessageDialog(mainFrame, "暂无可用的小组");
                        return;
                    }
                    int randomGroupIndex = new java.util.Random().nextInt(groupManager.getGroups().size());
                    JOptionPane.showMessageDialog(mainFrame, "随机抽取的小组是: " +
                            groupManager.getGroups().get(randomGroupIndex));
                    break;

                case 1: // 随机在小组中抽取学生
                    String[] groups = groupManager.getGroups().toArray(new String[0]);
                    if (groups.length == 0) {
                        JOptionPane.showMessageDialog(mainFrame, "暂无可用的小组");
                        return;
                    }
                    String selectedGroup = (String) JOptionPane.showInputDialog(mainFrame,
                            "选择小组:", "随机抽取小组成员",
                            JOptionPane.QUESTION_MESSAGE, null, groups, groups[0]);

                    if (selectedGroup != null && !studentManager.getStudents().isEmpty()) {
                        int randomStudentIndex = new java.util.Random().nextInt(studentManager.getStudents().size());
                        JOptionPane.showMessageDialog(mainFrame, "随机抽取的学生是: " +
                                studentManager.getStudents().get(randomStudentIndex));
                    }
                    break;

                case 2: // 随机在班级里抽取学生
                    if (studentManager.getStudents().isEmpty()) {
                        JOptionPane.showMessageDialog(mainFrame, "暂无可用的学生");
                        return;
                    }
                    int randomStudentIndex = new java.util.Random().nextInt(studentManager.getStudents().size());
                    JOptionPane.showMessageDialog(mainFrame, "随机抽取的学生是: " +
                            studentManager.getStudents().get(randomStudentIndex));
                    break;

                case 3: // 给小组评分
                    groups = groupManager.getGroups().toArray(new String[0]);
                    if (groups.length == 0) {
                        JOptionPane.showMessageDialog(mainFrame, "暂无可用的小组");
                        return;
                    }
                    selectedGroup = (String) JOptionPane.showInputDialog(mainFrame,
                            "选择要评分的小组:", "小组评分",
                            JOptionPane.QUESTION_MESSAGE, null, groups, groups[0]);

                    if (selectedGroup != null) {
                        String scoreStr = JOptionPane.showInputDialog(mainFrame, "请输入评分 (0-100):");
                        try {
                            int score = Integer.parseInt(scoreStr);
                            randomPicker.recordGroupScore(selectedGroup, score);
                            JOptionPane.showMessageDialog(mainFrame, "已记录小组 " + selectedGroup + " 的评分为: " + score);
                        } catch (NumberFormatException ex) {
                            JOptionPane.showMessageDialog(mainFrame, "请输入有效的数字");
                        }
                    }
                    break;

                case 4: // 记录学生考勤
                    String[] students = studentManager.getStudents().toArray(new String[0]);
                    if (students.length == 0) {
                        JOptionPane.showMessageDialog(mainFrame, "暂无可用的学生");
                        return;
                    }
                    String selectedStudent = (String) JOptionPane.showInputDialog(mainFrame,
                            "选择学生:", "记录考勤",
                            JOptionPane.QUESTION_MESSAGE, null, students, students[0]);

                    if (selectedStudent != null) {
                        String[] attendanceOptions = {"到场", "缺席"};
                        int attendance = JOptionPane.showOptionDialog(mainFrame,
                                "请选择考勤状态", "记录考勤",
                                JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
                                null, attendanceOptions, attendanceOptions[0]);

                        JOptionPane.showMessageDialog(mainFrame, "已记录 " + selectedStudent +
                                " 的考勤状态为: " + attendanceOptions[attendance]);
                    }
                    break;
// 在 randomButton 的 ActionListener 中，将 case 5 修改为：
                case 5: // 导出成绩
                    StringBuilder grades = new StringBuilder("成绩报告:\n\n");

                    // 获取学生成绩
                    grades.append("学生成绩:\n");
                    Map<String, Double> studentScores = randomPicker.getStudentScores();
                    if (studentScores.isEmpty()) {
                        grades.append("暂无学生成绩记录\n");
                    } else {
                        for (Map.Entry<String, Double> entry : studentScores.entrySet()) {
                            grades.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
                        }
                    }

                    // 获取小组成绩
                    grades.append("\n小组成绩:\n");
                    Map<String, Integer> groupScores = randomPicker.getGroupScores();
                    if (groupScores.isEmpty()) {
                        grades.append("暂无小组成绩记录\n");
                    } else {
                        for (Map.Entry<String, Integer> entry : groupScores.entrySet()) {
                            grades.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
                        }
                    }

                    JOptionPane.showMessageDialog(mainFrame, grades.toString(),
                            "成绩报告", JOptionPane.INFORMATION_MESSAGE);
                    break;
            }
        });

        // 退出按钮事件
        exitButton.addActionListener(e -> System.exit(0));

        // 添加按钮到主窗口
        mainFrame.add(classButton);
        mainFrame.add(groupButton);
        mainFrame.add(studentButton);
        mainFrame.add(randomButton);
        mainFrame.add(exitButton);

        // 显示主窗口
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);
    }
}